//
//  LFTabBar.swift
//  SDAutoWeibo
//
//  Created by 刘健 on 16/3/19.
//  Copyright © 2016年 Clemmie. All rights reserved.
//

import UIKit

let btnCounts: CGFloat = 5

class LFTabBar: UITabBar {
    
    
    var didComposeButton: (() -> ())?
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //定义下标
        var index: CGFloat = 0
        
        //平均宽度
        let with = frame.width / btnCounts
        
        //遍历tabBar
        for view in subviews {
            
            if view.isKindOfClass(NSClassFromString("UIControl")!) {
                
//                print("view:\(view)")
                
                view.frame = CGRectMake(index * with, 0, with, frame.height)
                
                index += index == 1 ? 2 : 1
                
            }
        }
        
        Button.frame = CGRectMake(2 * with, 0, with, frame.height)
        
    }
    
      //MARK: -- 闭包回调
     func didClickComposeButton() {
        //保存闭包 回调按钮点击事件
        didComposeButton?()
    }
    
    
    // MARK: -- 懒加载
    //添加按钮
    private lazy var Button: UIButton = {
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.addTarget(self, action: "didClickComposeButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        //添加到tabBar
        self.addSubview(btn)
        
        return btn
    }()
    
    
}
